﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for StaffBUS
/// </summary>
public class StaffBUS
{
    private StaffDA staff;

	public StaffBUS()
	{
		//
		// TODO: Add constructor logic here
		//
        staff = new StaffDA();
	}
    public DataSet staffLogin(string username, string pass)
    {
        return staff.staffLogin(username,pass);
    }
    public bool insertStudent(Student s)
    {
        return staff.insertStudent(s);
    }

    public bool updateStudent(Student s)
    {
        return staff.updateStudent(s);
    }

    /*
     * xoa Student co studentId
     * - tim tat ca Design co studentId
     * - voi 1 Design co studentId, lay duoc 1 designId
     * - xoa Mark co designId
     * - xoa Design co designId
     */
    //XONG
    public bool delStudent(Student s)
    {
        DataTable dt = staff.getDesignsByStudent(s).Tables[0];
        Design d = new Design();
        Mark m;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            d.Id = Convert.ToInt32(dt.Rows[i][0].ToString());
            m = staff.getMarkByDesign(d);
            if (!staff.delMark(m))
            {
                return false;
            }
            if (!staff.delDesign(d))
            {
                return false;
            }
        }
        return staff.delStudent(s);
    }

    public DataSet getAllStudent()
    {
        DataSet ds= staff.getAllStudent();
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }

    public DataSet searchStudent(string name)
    {
        DataSet ds= staff.searchStudent(name);
        //if (!(ds.Tables[0].Rows.Count > 0))
        //{
        //    return null;
        //}
        return ds;
    }
    public DataSet searchStudentByName(string name)
    {
        DataSet ds = staff.searchStudentByName(name);
        //if (!(ds.Tables[0].Rows.Count > 0))
        //{
        //    return null;
        //}
        return ds;
    }
    public bool insertMark(Mark m)
    {
        return staff.insertMark(m);
    }

    public bool updateMark(Mark m)
    {
        return staff.updateMark(m);
    }

    //doi voi Design pham quy, reset Mark=0
    public bool resetMark(Mark m)
    {
        return staff.resetMark(m);
    }

    public DataSet getAllDesign()
    {
        DataSet ds= staff.getAllDesign();
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }

    public DataSet getAllDesignOnCompetition(Competition c)
    {
        DataSet ds= staff.getAllDesignOnCompetitition(c);
        /*
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
         */
        return ds;
    }

    public DataSet searchDesignToDs(Design d)
    {
        return staff.searchDesign(d);
    }

    public Design searchDesign(Design d1)
    {
        ManagerBUS manager = new ManagerBUS();
        DataSet ds = staff.searchDesign(d1);
        Design d = new Design();
        DataRow dr = ds.Tables[0].Rows[0];
        d.Id = Convert.ToInt32(dr["designId"].ToString());
        d.Name = dr["name"].ToString();
        d.PostDate = dr["postDate"].ToString();
        d.Description = dr["description"].ToString();
        d.Quotation= dr["quotation"].ToString();
        d.DesignImage = dr["designImage"].ToString();
        DataTable dt1=searchStudent(dr[5].ToString()).Tables[0];
        Student st = new Student();
        st.Id=Convert.ToInt32(dt1.Rows[0][0].ToString());
        st.Username = dt1.Rows[0][1].ToString();
        st.Password = dt1.Rows[0][2].ToString();
        st.Fullname = dt1.Rows[0][3].ToString();
        st.Address = dt1.Rows[0][4].ToString();
        st.Email = dt1.Rows[0][5].ToString();
        st.PhoneNumber = dt1.Rows[0][6].ToString();
        st.Major = dt1.Rows[0][7].ToString();
        st._class1 = dt1.Rows[0][8].ToString();
        d.St = st;
        DataTable dt2 = manager.searchCompetition(dr[6].ToString()).Tables[0];
        Competition comp = new Competition();
        comp.ComId = Convert.ToInt32(dt2.Rows[0]["comId"].ToString());
        comp.Name = dt2.Rows[0]["name"].ToString();
        comp.FirstPrize = dt2.Rows[0]["firstPrize"].ToString();
        comp.SecondPrize = dt2.Rows[0]["secondPrize"].ToString();
        comp.ThirdPrize = dt2.Rows[0]["thirdPrize"].ToString();
        comp.PostDate = dt2.Rows[0]["postDate"].ToString();
        comp.StartDate = dt2.Rows[0]["startDate"].ToString();
        comp.EndDate = dt2.Rows[0]["endDate"].ToString();
        comp.Description = dt2.Rows[0]["description"].ToString();
        comp.Quotation = dt2.Rows[0]["quotation"].ToString();
        comp.Manager.Username = dt2.Rows[0]["userName"].ToString();
        //comp.CompImage = dt2.Rows[0]["compImage"].ToString();
        Manager m = manager.searchManager(dt2.Rows[0][8].ToString());
        comp.Manager = m;
        d.Com = comp;
        return d;
    }

    public DataSet getCompetitionAccess(int id)
    {
        return staff.getCompetitionAccess(id);
    }
}